package com.talkweb.historylib.dialog;

import com.talkweb.historylib.ResourceTable;
import com.talkweb.securitycamera.utils.TimeUtils;
import ohos.agp.components.*;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * @author SUQI
 * @date 2021/2/4
 * @description 展示筛选日期
 **/
public class SearchByDateDialog extends BaseCommonDialog {
    private static final int RADIUS_VP_20 = 20;
    private static final int MARGIN_VP_20 = 20;
    private Component mComponent;
    private Button btnConfirm;
    private Button btnCancel;
    private Text tvStartDate;
    private Text tvEndDate;
    private OnDateSelectionCompleteListener onDateSelectionCompleteListener;

    public SearchByDateDialog(Context context, OnDateSelectionCompleteListener onDateSelectionCompleteListener) {
        super(context);
        this.onDateSelectionCompleteListener = onDateSelectionCompleteListener;
    }

    @Override
    protected boolean isBottomDialog() {
        return true;
    }


    @Override
    protected void initialize(Context context) {
        super.initialize(context);
        setCornerRadius(AttrHelper.vp2px(RADIUS_VP_20, context));
        setSize(getDialogWidth(MARGIN_VP_20), ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mComponent = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_search_by_date, null, false);
        setContentCustomComponent(mComponent);
        initView();
    }

    private void initView() {
        tvStartDate = (Text) mComponent.findComponentById(ResourceTable.Id_tv_start_date);
        tvEndDate = (Text) mComponent.findComponentById(ResourceTable.Id_tv_end_date);
        btnConfirm = (Button) mComponent.findComponentById(ResourceTable.Id_dialog_confirm);
        btnCancel = (Button) mComponent.findComponentById(ResourceTable.Id_dialog_cancel);
        String currentDate = TimeUtils.millis2String(System.currentTimeMillis(), TimeUtils.FORMAT_PATTERN);
        tvStartDate.setText(currentDate);
        tvEndDate.setText(currentDate);
        tvStartDate.setClickedListener(component -> showSelectDate(tvStartDate, false));
        tvEndDate.setClickedListener(component -> showSelectDate(tvEndDate, true));
        btnCancel.setClickedListener(component -> hide());
        btnConfirm.setClickedListener(component -> {
            hide();
            if (onDateSelectionCompleteListener != null) {
                onDateSelectionCompleteListener.onDown(tvStartDate.getText(), tvEndDate.getText());
            }
        });
    }

    /**
     * 弹出日期选择
     *
     * @param date
     * @param isSelectedEndTime
     */
    private void showSelectDate(Text date, boolean isSelectedEndTime) {
        SelectDateDialog selectDateDialog = new SelectDateDialog(getContext(), (year, month, day) -> date.setText(String.format("%d-%d-%d", year, month, day)));
        if (isSelectedEndTime) {
            selectDateDialog.setStartDate(tvStartDate.getText());
        }
        selectDateDialog.show();
    }

    public interface OnDateSelectionCompleteListener {
        void onDown(String startDate, String endDate);
    }
}
